package com.api.gbhg.luoao.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.luoao.utils.CollectUtils;
import com.api.gbhg.luoao.utils.SummaryReturn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

@Path("/Returns")
public class ReturnsTheDepartment {

    private final Logger logger = LoggerFactory.getLogger(ReturnsTheDepartment.class);

    /**
     * 汇总上报返回科室列表
     *
     * @param param
     * @return
     */
    @POST
    @Path("/SummaryReturnlist")
    @Produces(MediaType.APPLICATION_JSON)
    public String getSummaryReturn(Map<String, Object> param) {
        RecordSet summaryReturn = CollectUtils.getSummaryReturn(param);
        Map<String, Object>[] list = new HashMap[summaryReturn.getArray().size()];
        int i = 0;
        while (summaryReturn.next()) {
            list[i] = new HashMap<>();
            list[i].put("requestId", summaryReturn.getStringNoTrim("requestId"));
            list[i].put("xmmc", summaryReturn.getStringNoTrim("xmmc"));
            list[i].put("sbje", summaryReturn.getStringNoTrim("sbje"));
            list[i].put("zpje", summaryReturn.getStringNoTrim("zpje"));
            list[i].put("bz", summaryReturn.getStringNoTrim("bz"));
            list[i].put("id", summaryReturn.getStringNoTrim("id"));
            list[i].put("hzsbid",summaryReturn.getStringNoTrim("hzsbid"));
            i++;
        }

        return JSON.toJSONString(list);
    }

    /**
     * 汇总上报返回科室页中的提交按钮
     *
     * @param maps
     * @return
     */
    @POST
    @Path("/submit")
    @Produces(MediaType.APPLICATION_JSON)
    public String getsubmit(Map<String, Object>[] maps) {
        boolean b = false;
        if (maps.length != 0 || maps != null) {
            //更新汇总上报放回科室表
            b = CollectUtils.Updatahzsbfhks(maps);//汇总上报返回科室表
            if (b) {
                Map<String, Object>[] processnode = CollectUtils.getProcessnode(maps);
                return JSON.toJSONString(processnode);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", b);

        return JSON.toJSONString(map);


    }

    /**
     * 是否显示汇总上报科室按钮
     *
     * @param param
     * @return
     */
    @POST
    @Path("/DepartmentButton")
    @Produces(MediaType.APPLICATION_JSON)
    public String display(@Context HttpServletRequest request, @Context HttpServletResponse response, Map<String, Object> param) {
        User user = HrmUserVarify.getUser(request, response);  //得到当前用户
        int userId = user.getUID(); //得到当前用户id

        Map<String, String> map = new HashMap<>();

        //判断是否是独立核算单位
        RecordSet getunit = new RecordSet();
        getunit.executeQuery("select * from hrmresource where id = " + userId);
        logger.info("是否显示汇总上报科室按钮=" + "select * from hrmresource where id = " + userId);
        if (getunit.next()) {
            String lastname = getunit.getString("lastname");
            if (lastname.equals("李升亮")) {
                map.put("all", "noshow");
                return JSON.toJSONString(map);
            }
        }
        String dw = param.getOrDefault("dw", "").toString();
        String lclx = param.getOrDefault("lx", "").toString();
        //返回科室是否可以点击查询汇总表
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from uf_hzsbxm " +
                "where dw = " + dw + " " +
                "and lx = " + lclx + " and shzt = 2");

        //查看返回科室是否有数据
        RecordSet sr = new RecordSet();
        sr.executeQuery(" select * from uf_hzsbfhks ks ," +
                " (select MAX(sj)sj from uf_hzsbfhks where sbdw = " + dw + " and lclx = " + lclx + " and xgzt = 0 )js " +
                "where ks.sj =js.sj and sbdw = " + dw + " and lclx = " + lclx + " and xgzt = 0");
        map.put("department", "noshow");
//        if (rs.next()) {
//            if (sr.next()) {
//
//                map.put("department", "show");
//            }
//        } else {
//
//        }
        //汇总上报是否可以点击
        //boolean getnd = CollectUtils.getnd(param);
        map.put("report", "show");
//        if (true) {
//            map.put("report", "show");
//        } else {
//            boolean getshzt = CollectUtils.getshzt(param);
//            if (getshzt) {
//                map.put("report", "show");
//            } else {
//                map.put("report", "noshow");
//            }
//        }
        return JSON.toJSONString(map);
    }


    /**
     * 流程表单中的 批准 回退 转发 提交按钮
     * 根据当前用户判断是否 是独立核算单位还是非独立核算单位
     *
     * @param param
     * @return
     */
    @POST
    @Path("/Processbutton")
    @Produces(MediaType.APPLICATION_JSON)
    public String button(Map<String, Object> param) {
        Map<String, String> maps = new HashMap<>();
        RecordSet getunit = CollectUtils.getunit(param);
        //判断是否是独立核算单位
        if (getunit.next()) {
            maps.put("result", "noshow");
        } else {
            maps.put("result", "show");
        }
        return JSON.toJSONString(maps);
    }


}
